home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / grpmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  2.6 KB  |  132 lines

  1. /* UNIX group file access module */
  2.  
  3. #include "Python.h"
  4.  
  5. #include <sys/types.h>
  6. #include <grp.h>
  7.  
  8. #ifdef AMITCP
  9. #include <proto/usergroup.h>
  10. #endif
  11. #ifdef INET225
  12. #include <proto/socket.h>
  13. #endif
  14.  
  15. #include "protos/grpmodule.h"
  16.  
  17.  
  18. static PyObject *mkgrent(p)
  19.     struct group *p;
  20. {
  21.     PyObject *v, *w;
  22.     char **member;
  23.     if ((w = PyList_New(0)) == NULL) {
  24.         return NULL;
  25.     }
  26.     for (member = p->gr_mem; *member != NULL; member++) {
  27.         PyObject *x = PyString_FromString(*member);
  28.         if (x == NULL || PyList_Append(w, x) != 0) {
  29.             Py_XDECREF(x);
  30.             Py_DECREF(w);
  31.             return NULL;
  32.         }
  33.         Py_DECREF(x);
  34.     }
  35.     v = Py_BuildValue("(sslO)",
  36.                p->gr_name,
  37.                p->gr_passwd,
  38. #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
  39. /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
  40.    for later versions you may have to remove this */
  41.                (long)p->gr_short_pad, /* ugh-NeXT broke the padding */
  42. #else
  43.                (long)p->gr_gid,
  44. #endif
  45.                w);
  46.     Py_DECREF(w);
  47.     return v;
  48. }
  49.  
  50. static PyObject *grp_getgrgid(self, args)
  51.     PyObject *self, *args;
  52. {
  53.     int gid;
  54.     struct group *p;
  55.     if (!PyArg_Parse((args),"i",(&gid)))
  56.         return NULL;
  57.     if ((p = getgrgid(gid)) == NULL) {
  58.         PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
  59.         return NULL;
  60.     }
  61.     return mkgrent(p);
  62. }
  63.  
  64. static PyObject *grp_getgrnam(self, args)
  65.     PyObject *self, *args;
  66. {
  67.     char *name;
  68.     struct group *p;
  69.     if (!PyArg_Parse((args),"s",(&name)))
  70.         return NULL;
  71.     if ((p = getgrnam(name)) == NULL) {
  72.         PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
  73.         return NULL;
  74.     }
  75.     return mkgrent(p);
  76. }
  77.  
  78. static PyObject *grp_getgrall(self, args)
  79.     PyObject *self, *args;
  80. {
  81.     PyObject *d;
  82.     struct group *p;
  83.     if (!PyArg_NoArgs(args))
  84.         return NULL;
  85.     if ((d = PyList_New(0)) == NULL)
  86.         return NULL;
  87. #if !defined(AMITCP) && !defined(INET225)
  88.     setgrent();
  89.     while ((p = getgrent()) != NULL) {
  90.         PyObject *v = mkgrent(p);
  91.         if (v == NULL || PyList_Append(d, v) != 0) {
  92.             Py_XDECREF(v);
  93.             Py_DECREF(d);
  94.             return NULL;
  95.         }
  96.         Py_DECREF(v);
  97.     }
  98.     return d;
  99. #else
  100.  #ifdef AMITCP
  101.     setgrent();
  102.  #else
  103.     setgrent(1); /* INET225 wants argument XXX correct? */
  104.  #endif
  105.     while ((p = getgrent()) != NULL) {
  106.         PyObject *v = mkgrent(p);
  107.         if (v == NULL || PyList_Append(d, v) != 0) {
  108.             Py_XDECREF(v);
  109.             Py_DECREF(d);
  110.             endgrent();
  111.             return NULL;
  112.         }
  113.         Py_DECREF(v);
  114.     }
  115.     endgrent();
  116.     return d;
  117. #endif /* AMITCP or INET225 */
  118. }
  119.  
  120. static PyMethodDef grp_methods[] = {
  121.     {"getgrgid",    grp_getgrgid},
  122.     {"getgrnam",    grp_getgrnam},
  123.     {"getgrall",    grp_getgrall},
  124.     {NULL,        NULL}        /* sentinel */
  125. };
  126.  
  127. DL_EXPORT(void)
  128. initgrp()
  129. {
  130.     Py_InitModule("grp", grp_methods);
  131. }
  132.